package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.Common;
import com.group3.dao.LeaveDetailMapper;
import com.group3.dao.LeaveMapper;
import com.group3.domain.LeaveDetail;
import com.group3.domain.LeaveDetailExample;
import com.group3.domain.LeaveExample;
import com.group3.domain.LeaveOperationRecord;
import com.group3.exception.BusinessException;
import com.group3.exception.ExceptionEnum;
import com.group3.pojo.receive.LeaveReceive;
import com.group3.pojo.result.LeaveDetailResult;
import com.group3.service.ALeaveService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * ALeaveServiceImpl
 *
 * @author HY
 * 2021/06/16 18:17
 **/


@Service
public class ALeaveServiceImpl implements ALeaveService {

    @Resource
    private LeaveDetailMapper leaveDetailMapper;
    @Resource
    private Common common;

    @Override
    @Transactional
    public Result<List<LeaveDetail>> selectLeave(LeaveReceive leaveReceive) {

        LeaveDetailExample leaveDetailExample = new LeaveDetailExample();
        LeaveDetailExample.Criteria criteria = leaveDetailExample.createCriteria();

        //提交开始时间
        if (NotNull.judge(leaveReceive.getStartTime())){
            criteria.andSubmitTimeGreaterThan(leaveReceive.getStartTime());
        }
        //提交结束时间
        if (NotNull.judge(leaveReceive.getEndTime())){
            criteria.andSubmitTimeLessThan(leaveReceive.getEndTime());
        }
        //院系
        if (NotNull.judge(leaveReceive.getProfessionName())){
            criteria.andProfessionNameEqualTo(leaveReceive.getProfessionName());
        }
        //专业
        if (NotNull.judge(leaveReceive.getDepartName())){
            criteria.andDepartNameEqualTo(leaveReceive.getDepartName());
        }
        //年级
        if (NotNull.judge(leaveReceive.getGrade())){
            criteria.andGradeEqualTo(leaveReceive.getGrade());
        }
        //班级
        if (NotNull.judge(leaveReceive.getClassId())){
            criteria.andClassIdEqualTo(leaveReceive.getClassId());
        }
//        //小组
//        if (leaveReceive.getStartTime() != null){
//            criteria.andSubmitTimeGreaterThan(leaveReceive.getStartTime());
//        }
        //学号
        if (NotNull.judge(leaveReceive.getStudentId())){
            criteria.andStudentIdEqualTo(leaveReceive.getStudentId());
        }
        //学员姓名
        if (NotNull.judge(leaveReceive.getStudentName())){
            criteria.andStudentNameLike("%"+(leaveReceive.getStudentName())+"%");
        }
        //主题
        if (NotNull.judge(leaveReceive.getTheme())){
            criteria.andThemeLike("%"+(leaveReceive.getTheme())+"%");
        }
        //请假类型
        if (NotNull.judge(leaveReceive.getType())){
            criteria.andTypeEqualTo(leaveReceive.getType());
        }
        //请假原因
        if (NotNull.judge(leaveReceive.getReason())){
            criteria.andReasonLike("%"+(leaveReceive.getReason())+"%");
        }
        //审批状态
        if (NotNull.judge(leaveReceive.getState())){
            criteria.andStateEqualTo(leaveReceive.getState());
        }
        //审批人
        if (NotNull.judge(leaveReceive.getTeacherId())){
            criteria.andTeacherIdEqualTo(leaveReceive.getTeacherId());
        }

//        1.先开启分页
        PageHelper.startPage(leaveReceive.getPageNumber(), leaveReceive.getPageSize());//启动分页
//        2.执行SQL语句
        List<LeaveDetail> leaveDetails = leaveDetailMapper.selectByExample(leaveDetailExample);
//        3.获取分页信息
        PageInfo<LeaveDetail> personalScoresPageInfo = new PageInfo<>(leaveDetails);//获取分页信息

        return ResultUtil.success("查询成功", leaveDetails, personalScoresPageInfo.getTotal());
    }

    @Override
    @Transactional
    public Result<LeaveDetailResult> showLeave(Integer leaveId) {

        LeaveDetailExample leaveDetailExample = new LeaveDetailExample();
        leaveDetailExample.createCriteria().andLeaveIdEqualTo(leaveId);

        List<LeaveDetailResult> leaveDetailResults = leaveDetailMapper.SelectLeaveInfo(leaveDetailExample);

        if (leaveDetailResults == null){
            throw new BusinessException(ExceptionEnum.DATABASE_OPERATION_ERR);
        }
//        List<LeaveOperationRecord> list =
//                common.SelectOperationRecordByLeaveId(leaveResults.get(0).getLeaveId());
//        leaveResults.get(0).setLeaveOperationRecords(list);
        return ResultUtil.success("查询成功", leaveDetailResults);
    }
}
